<!-- 收藏文章按钮 -->
<template>
  <van-tabbar-item v-if="!this.modelValue" @click="onCollect">
    <span>收藏</span>
    <template #icon>
      <van-icon name="star-o" />
    </template>
  </van-tabbar-item>
  <van-tabbar-item v-else @click="onCollect" class="collected">
    <span>已收藏</span>
    <template #icon>
      <van-icon name="star" />
    </template>
  </van-tabbar-item>
</template>

<script>
import { collectArticle, deleteCollect } from '../api/user'
export default {
  name: 'CollectArticle',
  props: {
    modelValue: {
      type: Boolean,
      required: true
    },
    articleId: {
      type: [Number, String, Object],
      required: true
    }
  },
  data () {
    return {
    }
  },
  methods: {
    async onCollect () {
      try {
        if (this.modelValue) {
          // 收藏状态，点击取消收藏
          await deleteCollect(this.articleId)
        } else {
          // 没有收藏状态,点击收藏
          await collectArticle({ target: this.articleId })
        }
        this.$emit('update:modelValue', !this.modelValue)
        this.$toast.success(!this.modelValue ? '收藏成功' : '取消收藏')
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style  scoped lang='less'>
/deep/.collected {
  color: aqua !important;
}
</style>
